package com.coolbi.security.biz.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.security.biz.RoleToUrlBiz;
import com.coolbi.security.dao.RoleToUrlDao;
import com.coolbi.security.entity.RoleToUrl;
import com.coolbi.security.entity.RoleUrl;
import com.coolbi.security.entity.Roles;

public class RoleToUrlBizImpl implements RoleToUrlBiz {

	@Autowired
	private RoleToUrlDao roleToUrlDao;
	public void setRoleToUrlDao(RoleToUrlDao roleToUrlDao) {
		this.roleToUrlDao = roleToUrlDao;
	}
	
	@Override
	public List<RoleToUrl> find(Map<String, Object> parameter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RoleToUrl> find(Map<String, Object> parameter, int offset,
			int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int count(Map<String, Object> parameter) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int countByKeyword(String keyword) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<RoleToUrl> search(String keyword, int offset, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public RoleToUrl save(RoleToUrl obj) {
		return roleToUrlDao.save(obj);
	}

	@Override
	public int update(RoleToUrl obj) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delete(long id) {
		return roleToUrlDao.delete(id);
	}

	@Override
	public RoleToUrl get(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RoleToUrl> list() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RoleToUrl> list(int offset, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Roles> findRoleByUrlId(long id) {
		return roleToUrlDao.findRoleByUrlId(id);
	}

	@Override
	public List<RoleUrl> findUrlByRoleId(long id) {
		return roleToUrlDao.findUrlByRoleId(id);
	}

	@Override
	public int deleteByUrlId(long id) {
		return roleToUrlDao.deleteByUrlId(id);
	}

	@Override
	public void saveUrlAuthToRole(Long[] authArray, Long[] abolishArray,
			RoleToUrl roleToUrl) {
		List<Long> urlIds = roleToUrlDao.findByUrlsAndRoleId(authArray, roleToUrl.getRoleId());
		if (null != abolishArray) {
			roleToUrlDao.deleteByUrlIds(abolishArray, roleToUrl.getRoleId());
		}
		if (null != authArray) {
			for (int i = 0; i < authArray.length; i ++) {
				if (urlIds.size() > 0) {
					if (!urlIds.contains(authArray[i])) {
						roleToUrl.setUrlId(authArray[i]);
						roleToUrlDao.save(roleToUrl);
					}
				} else {
					roleToUrl.setUrlId(authArray[i]);
					roleToUrlDao.save(roleToUrl);
				}
			}
		}
	}
	
}
